﻿namespace QuickMail.BusinessLogic
{
    using System.Net.Mail;
    using QuickMail.DataModels;
    using QuickMail.Properties;

    public static class ConnectionSetupWorker
    {

        /// <summary>
        /// Seeks server in the current listing.
        /// </summary>
        /// <returns>true - found, false - not found</returns>
        public static bool DetectServer(string fullName, string email, string password)
        {
            var tempEmail = new MailAddress(email);

            switch (tempEmail.Host)
            {
                case "gmail.com":
                    SetGmailComSettings(fullName, email, password);
                    return true;
                case "yandex.ru":
                    SetYandexRuSettings(fullName, email, password);
                    return true;
                case "mail.ru":
                    SetMailRuSettings(fullName, email, password);
                    return true;
            }

            SetDefaultSettings(fullName, email, password);
            return false;
        }

        private static void SetGmailComSettings(string fullName, string email, string password)
        {
            Settings.Default.FullName = fullName;
            Settings.Default.GetServer = "imap.gmail.com";
            Settings.Default.GetServerType = (int)GetServers.Imap;
            Settings.Default.GetPort = 993;
            Settings.Default.GetUseSsl = true;
            Settings.Default.GetEmail = email;
            Settings.Default.GetPassword = CryptWorker.Encrypt(password);

            Settings.Default.SendServer = "smtp.gmail.com";
            Settings.Default.SendPort = 465;
            Settings.Default.SendCryptType = (int)CryptTypes.Ssl;
            Settings.Default.SendEmail = email;
            Settings.Default.SendPassword = CryptWorker.Encrypt(password);
            Settings.Default.Save();
        }

        private static void SetYandexRuSettings(string fullName, string email, string password)
        {
            Settings.Default.FullName = fullName;
            Settings.Default.GetServer = "imap.yandex.ru";
            Settings.Default.GetServerType = (int)GetServers.Imap;
            Settings.Default.GetPort = 993;
            Settings.Default.GetUseSsl = true;
            Settings.Default.GetEmail = email;
            Settings.Default.GetPassword = CryptWorker.Encrypt(password);

            Settings.Default.SendServer = "smtp.yandex.ru";
            Settings.Default.SendPort = 465;
            Settings.Default.SendCryptType = (int)CryptTypes.Ssl;
            Settings.Default.SendEmail = email;
            Settings.Default.SendPassword = CryptWorker.Encrypt(password);
            Settings.Default.Save();
        }

        private static void SetMailRuSettings(string fullName, string email, string password)
        {
            Settings.Default.FullName = fullName;
            Settings.Default.GetServer = "imap.mail.ru";
            Settings.Default.GetServerType = (int)GetServers.Imap;
            Settings.Default.GetPort = 993;
            Settings.Default.GetUseSsl = true;
            Settings.Default.GetEmail = email;
            Settings.Default.GetPassword = CryptWorker.Encrypt(password);

            Settings.Default.SendServer = "smtp.mail.ru";
            Settings.Default.SendPort = 465;
            Settings.Default.SendCryptType = (int)CryptTypes.Ssl;
            Settings.Default.SendEmail = email;
            Settings.Default.SendPassword = CryptWorker.Encrypt(password);
            Settings.Default.Save();
        }

        private static void SetDefaultSettings(string fullName, string email, string password)
        {
            var mailAddress = new MailAddress(email);
            Settings.Default.FullName = fullName;
            Settings.Default.GetServer = "imap." + mailAddress.Host;
            Settings.Default.GetServerType = (int)GetServers.Imap;
            Settings.Default.GetPort = 993;
            Settings.Default.GetUseSsl = true;
            Settings.Default.GetEmail = email;
            Settings.Default.GetPassword = CryptWorker.Encrypt(password);

            Settings.Default.SendServer = "smtp." + mailAddress.Host;
            Settings.Default.SendPort = 465;
            Settings.Default.SendCryptType = (int)CryptTypes.Ssl;
            Settings.Default.SendEmail = email;
            Settings.Default.SendPassword = CryptWorker.Encrypt(password);
            Settings.Default.Save();
        }
    }
}
